package views

import (
	"errors"
	"go_web/common"
	"go_web/service"
	"net/http"
	"strconv"
	"strings"
)

func (h *HTMLApi) ArticleDetail(w http.ResponseWriter, r *http.Request) {

	detail := common.Template.Detail
	//获取路径参数
	//http://localhost:8080/p/1.html
	path := r.URL.Path
	pIdStr := strings.TrimPrefix(path, "/p/")
	pIdStr = strings.TrimSuffix(pIdStr, ".html")

	pId, err := strconv.Atoi(pIdStr)
	if err != nil {
		detail.WriteError(w, errors.New("路径不匹配"))
		return
	}

	postRes, err := service.GetPostDetail(pId)
	if err != nil {
		detail.WriteError(w, errors.New("路径不匹配"))
		return
	}

	detail.WriteData(w, postRes)
}
